package com.deyuanyun.pic.business.service.impl;

import java.util.Arrays;
import java.util.List;

import org.springframework.stereotype.Service;

import com.deyuanyun.pic.business.domain.MileagePoint;
import com.deyuanyun.pic.business.service.NagmStationService;

@Service
public class NagmStationServiceImpl implements NagmStationService{

    @Override
    public MileagePoint[] findNagmStation(List<MileagePoint> stationList,Long[] mileages) {
        //并且处理排序的情况
        //Arrays.sort(mileages);
        MileagePoint[] nagmStations = new MileagePoint[mileages.length];
        for (int i = 0,j = 0; i < mileages.length; i++) {
            MileagePoint station = null;
            Long mileage = mileages[i];
            if(mileage != null){
                for (; j < stationList.size(); j++) {
                    MileagePoint station1 = stationList.get(j);
                    if(j == stationList.size() - 1){
                        //最后一个元素的时候
                        station = station1;
                        j--;
                        break;
                    }
                    MileagePoint station2 = stationList.get(j + 1);
                    long nagmMileage1 = Math.abs(station1.getMileage() - mileages[i]);//第一个点
                    long nagmMileage2 = Math.abs(station2.getMileage() - mileages[i]);//第二个点
                    if(nagmMileage1 < nagmMileage2){//第一个点小于第二个点，就表示第一个表是属于它最近的测试桩
                        station = station1;
                        j--;
                        break;
                    }
                }
            }
            nagmStations[i] = station;
        }
        return nagmStations;
    }

    
}
